<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //先请求a再请求b再请求c
        async function getData(){
            const user = await new Promise((resolve,reject)=>{
                try{
                    // throw new Error('a错误')
                    setTimeout(()=>{
                        console.log('a数据请求成功');
                        const user = {
                            username:'peihua'
                        }
                        resolve(user)
                    },2000)
                }
                catch(e){
                    reject(e.message)
                }
            });

            const sex = await new Promise((resolve,reject)=>{
                try{
                    // throw new Error('b错误')
                    setTimeout(()=>{
                        console.log('b数据请求成功');
                        const sex = {
                            sex:'男'
                        }
                        resolve({...user,
                        ...sex})
                    },2000)
                }
                catch(e){
                    reject(e.message)
                }
            });
            const age = await new Promise((resolve,reject)=>{
                try{
                    // throw new Error('b错误')
                    setTimeout(()=>{
                        console.log('c数据请求成功');
                        const age = {
                            sex:'22'
                        }
                        resolve({...user,
                        ...sex,...age})
                    },2000)
                }
                catch(e){
                    reject(e.message)
                }
            });

            //最后一次异步不需要再使用await
            console.log(`完整的数据是${JSON.stringify(age)}`);
        }
        getData()
    </script>
</body>
</html>